package org.dromara.system.controller.system;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.bean.domain.bo.HisGoodsOrderEvaluateBo;
import org.dromara.bean.domain.vo.HisActivityVo;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.file.FileUtils;
import org.dromara.system.domain.excel.GoodsOrderExcel;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.bean.domain.vo.HisGoodsOrderVo;
import org.dromara.bean.domain.bo.HisGoodsOrderBo;
import org.dromara.bean.service.IHisGoodsOrderService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商城订单
 *
 * @author Lion Li
 * @date 2024-12-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/goodsOrder")
public class HisGoodsOrderController extends BaseController {

    private final IHisGoodsOrderService hisGoodsOrderService;


    /**
     * 查询商城订单列表
     */
    @SaCheckPermission("system:goodsOrder:list")
    @GetMapping("/list")
    public TableDataInfo<HisGoodsOrderVo> list(HisGoodsOrderBo bo, PageQuery pageQuery) {
        return hisGoodsOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商城订单列表
     */
    @SaCheckPermission("system:goodsOrder:export")
    @Log(title = "商城订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HisGoodsOrderBo bo, HttpServletResponse response) {
        List<GoodsOrderExcel> excels = new ArrayList<>();
        //list转换List<Map<String, Object>> data
        List<HisGoodsOrderVo> list = hisGoodsOrderService.queryList(bo);
        list.forEach(obj->{
            GoodsOrderExcel goodsOrderExcel = new GoodsOrderExcel();
            goodsOrderExcel.setOrderNumber(obj.getOrderNumber());
            goodsOrderExcel.setMemberName(obj.getMemberName());
            goodsOrderExcel.setFullAddress(obj.getFullAddress());
            goodsOrderExcel.setTotalPrice(obj.getTotalPrice());
            goodsOrderExcel.setPayPrice(obj.getPayPrice());
            goodsOrderExcel.setPayTime(obj.getPayTime());
            goodsOrderExcel.setDeliveryTime(obj.getDeliveryTime());
            goodsOrderExcel.setStatus(obj.getStatus());
            String result = obj.getGoodsOrderDetails().stream()
                .map(r -> r.getGoods().getTitle() + "  数量：" + r.getCount())
                .collect(Collectors.joining("\n"));
            goodsOrderExcel.setGoods(result);
            excels.add(goodsOrderExcel);
        });


        ExcelUtil.exportExcel(excels, "活动", GoodsOrderExcel.class, response);

    }

    /**
     * 获取商城订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:goodsOrder:query")
    @GetMapping("/{id}")
    public R<HisGoodsOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(hisGoodsOrderService.queryById(id));
    }


    /**
     * 删除商城订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:goodsOrder:remove")
    @Log(title = "商城订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(hisGoodsOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 确认发货
     *
     * @param bo
     */
    @SaCheckPermission("system:goodsOrder:edit")
    @Log(title = "确认发货", businessType = BusinessType.UPDATE)
    @PutMapping("/delivery")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HisGoodsOrderBo bo) {
        return toAjax(hisGoodsOrderService.updateByBo(bo));
    }

}
